﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS
{
    public partial class Ins_ListTA : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string strProvider = Resource1.databaseConnection;
        string id;

        protected void Page_Load(object sender, EventArgs e)
        {
            /*List Ta Highlihted*/
            ((Menu)Master.FindControl("Menu1")).Items[1].Selected = true;

            id = (string)Session["userName"];

            if (id == null)
                Response.Redirect("../login.aspx");

            // Connect to the Database
            conn = new MySqlConnection(strProvider);
            conn.Open();

            string strSQL = "SELECT GS.s_name AS \"Name\", GS.sid \"Id\", GS.s_email \"E-mail\", SC.cid \"Course\", SC.sectionNo \"Section\", GS.s_entryDate \"Entry Date\" " +
                            "FROM GradStudent GS, Educates E, SectionConsists SC " + 
                            "WHERE E.fsid = " + id + " AND E.ta_sid = GS.sid AND SC.secCorsId = E.secCorsId";

            sqlCommand = new MySqlCommand(strSQL, conn);
            readVals = sqlCommand.ExecuteReader();

            if (!IsPostBack)
            {
                MainTable.DataSource = readVals;
                MainTable.DataBind();
                MainTable.SelectedIndex = 0;
            }

            conn.Close();
        }

        protected void details_clicked(object sender, EventArgs e)
        {

            GridViewRow clickedRow = ((Button)sender).NamingContainer as GridViewRow;

            ClientScript.RegisterStartupScript(this.GetType(), "CloseWin", "<script>window.close()</script>");
            // id yi alıyor cell in 1. indexi
            string key = clickedRow.Cells[2].Text;

            string url = "../Details.aspx?type=student&id=" + key;

            ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>MyWindow=window.open('" + url + "','MyWindow','toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=no,width=1000,height=600'); return false;</script>"); 
        }
        
        protected void select_clicked(object sender, EventArgs e)
        {
        }

        protected void view_clicked(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("Ins_ViewSchedule.aspx?id=" + MainTable.SelectedRow.Cells[2].Text);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                ErrorLabel.Text = "Please Select A Graduate Student First!";
            }

        }

    }
}